import { defineConfig } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import { visualizer } from "rollup-plugin-visualizer";
import AutoImport from "unplugin-auto-import/vite";
import path from "path";
export default defineConfig(async () => {
	const UnoCss = await import("unocss/vite").then((i) => i.default);

	return {
		plugins: [
			uni(),
			visualizer(),
			UnoCss(),
			{
				name: "static-resource-redirect",
				resolveId(source) {
					if (source.startsWith("/static/")) {
						return path.resolve(__dirname, "." + source); // 把/static/重定向到项目根目录
					}
				},
			},
			AutoImport({
				imports: [
					{
						"@/utils/path": ["resolveStaticPath"],
						"@/utils/storage": ["useUniversalStorage"],
					},
				],
				dts: "src/auto-imports.d.ts",
			}),
		],
		base: process.env.VITE_APP_ENV === "production" ? "/static" : "./",
		resolve: {
			alias: {
				"@": path.resolve(__dirname, "./src"), // 核心配置
				"@pages": path.resolve(__dirname, "./src/pages"),
				components: path.resolve(__dirname, "./src/components"), // 可选扩展
				"@static": path.resolve(__dirname, "./static"), // 添加静态资源别名
			},
		},
		css: {
			preprocessorOptions: {
				scss: {
					// 取消sass废弃API的报警
					silenceDeprecations: [
						"legacy-js-api",
						"color-functions",
						"import",
					],
				},
			},
		},
		server: {
			port: 5100,
			fs: {
				// Allow serving files from one level up to the project root
				allow: [".."],
			},
			hmr: {
				overlay: false, // 禁用错误覆盖层
			},
		},
		optimizeDeps: {
			include: ["echarts/core", "echarts/charts", "echarts/components"],
		},
		build: {
			assetsInclude: ["**/*.png", "**/*.jpg"],
			assetsDir: "static", // 确保静态资源输出目录正确
			rollupOptions: {
				output: {
					assetFileNames: (assetInfo) => {
						return assetInfo.name === "favicon.ico"
							? "[name][extname]" // 特殊文件不哈希
							: "static/[name].[hash][extname]";
					},
				},
				input: {
					main: path.resolve(__dirname, "src/main.ts"),
				},
			},
		},
		define: {
			__VUE_I18N_FULL_INSTALL__: true, // 完整安装
			__VUE_I18N_LEGACY_API__: false, // 禁用遗留API
			__INTLIFY_PROD_DEVTOOLS__: false, // 生产环境禁用devtools
		},
	};
});
